ALTER PROC sp_save_forecast_user_defined_fields ( 
			@userList1Name varchar(100),
			@userDate1Name varchar(100),
			@userTxt1Name  varchar(100),
			@userTxt2Name  varchar(100) ) AS

DECLARE @fcUserEntry_rowId int
DECLARE @fcEntrySetup_rowId int
DECLARE @old_userList1Name varchar(100), @old_userDate1Name varchar(100), @old_userTxt1Name varchar(100), @old_userTxt2Name varchar(100)

	SELECT TOP 1 @fcUserEntry_rowId = RowId, @old_userList1Name = UserList1, @old_userDate1Name = UserDate1, @old_userTxt1Name = UserTxt1, @old_userTxt2Name = UserTxt2 from FCUSERENTRY
	IF ( @fcUserEntry_rowId is Null OR @fcUserEntry_rowId <= 0 )
	BEGIN
		INSERT INTO FCUSERENTRY ( UserList1, UserDate1, UserTxt1, UserTxt2 ) VALUES ( @userList1Name, @userDate1Name, @userTxt1Name, @userTxt2Name )

		IF ( @userList1Name is Not Null AND RTRIM( LTRIM(@userList1Name) ) != '' )
		BEGIN
			INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userList1Name, 1 )
		END
		IF ( @userDate1Name is Not Null AND RTRIM( LTRIM(@userDate1Name) ) != '' )
		BEGIN
			INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userDate1Name, 1 )
		END
		IF ( @userTxt1Name is Not Null AND RTRIM( LTRIM(@userTxt1Name) ) != '' )
		BEGIN
			INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userTxt1Name, 1 )
		END
		IF ( @userTxt2Name is Not Null AND RTRIM( LTRIM(@userTxt2Name) ) != '' )
		BEGIN
			INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userTxt2Name, 1 )
		END
	END
	ELSE
	BEGIN
		UPDATE FCUSERENTRY SET UserList1 = @userList1Name, UserDate1 = @userDate1Name, UserTxt1 = @userTxt1Name, UserTxt2 = @userTxt2Name WHERE RowId = @fcUserEntry_rowId
		UPDATE FCUENTRYVAL SET ENTRYNAME = @userList1Name WHERE TABLENAME = 'FCUSERENTRY' AND ENTRYNAME = @old_userList1Name
		SET @fcEntrySetup_rowId = NULL
		SELECT @fcEntrySetup_rowId = RowId FROM FCENTRYSETUP WHERE EntryName = @old_userList1Name
		IF ( @fcEntrySetup_rowId IS NOT NULL AND @fcEntrySetup_rowId > 0 )
		BEGIN
			UPDATE FCENTRYSETUP SET EntryName = @userList1Name WHERE RowId = @fcEntrySetup_rowId
			UPDATE EDITCOLDISP SET COLNAME = @userList1Name WHERE TABLENAME = 'FCENTRYSETUP' AND COLNAME = @old_userList1Name
		END
		ELSE
		BEGIN
			IF ( @userList1Name is Not Null AND RTRIM( LTRIM(@userList1Name) ) != '' )
			BEGIN
				INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userList1Name, 1 )
			END
		END
		
		SET @fcEntrySetup_rowId = NULL
		SELECT @fcEntrySetup_rowId = RowId FROM FCENTRYSETUP WHERE EntryName = @old_userDate1Name
		IF ( @fcEntrySetup_rowId IS NOT NULL AND @fcEntrySetup_rowId > 0 )
		BEGIN
			UPDATE FCENTRYSETUP SET EntryName = @userDate1Name WHERE RowId = @fcEntrySetup_rowId
			UPDATE EDITCOLDISP SET COLNAME = @userDate1Name WHERE TABLENAME = 'FCENTRYSETUP' AND COLNAME = @old_userDate1Name
		END
		ELSE
		BEGIN
			IF ( @userDate1Name is Not Null AND RTRIM( LTRIM(@userDate1Name) ) != '' )
			BEGIN
				INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userDate1Name, 1 )
			END
		END

		SET @fcEntrySetup_rowId = NULL
		SELECT @fcEntrySetup_rowId = RowId FROM FCENTRYSETUP WHERE EntryName = @old_userTxt1Name
		IF ( @fcEntrySetup_rowId IS NOT NULL AND @fcEntrySetup_rowId > 0 )
		BEGIN
			UPDATE FCENTRYSETUP SET EntryName = @userTxt1Name WHERE RowId = @fcEntrySetup_rowId
			UPDATE EDITCOLDISP SET COLNAME = @userTxt1Name WHERE TABLENAME = 'FCENTRYSETUP' AND COLNAME = @old_userTxt1Name
		END
		ELSE
		BEGIN
			IF ( @userTxt1Name is Not Null AND RTRIM( LTRIM(@userTxt1Name) ) != '' )
			BEGIN
				INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userTxt1Name, 1 )
			END
		END

		SET @fcEntrySetup_rowId = NULL
		SELECT @fcEntrySetup_rowId = RowId FROM FCENTRYSETUP WHERE EntryName = @old_userTxt2Name
		IF ( @fcEntrySetup_rowId IS NOT NULL AND @fcEntrySetup_rowId > 0 )
		BEGIN
			UPDATE FCENTRYSETUP SET EntryName = @userTxt2Name WHERE RowId = @fcEntrySetup_rowId
			UPDATE EDITCOLDISP SET COLNAME = @userTxt2Name WHERE TABLENAME = 'FCENTRYSETUP' AND COLNAME = @old_userTxt2Name
		END
		ELSE
		BEGIN
			IF ( @userTxt2Name is Not Null AND RTRIM( LTRIM(@userTxt2Name) ) != '' )
			BEGIN
				INSERT INTO FCENTRYSETUP ( EntryName, EntrySelect ) VALUES ( @userTxt2Name, 1 )
			END
		END

		exec sp_synchronize_editcoldisp
		
	END